<?php

class Product extends BaseController {

    public $product;

    function __construct() {
        parent::__construct();
        $this->product = new MProduct();
    }

    function uploadFile() {
        $upload = new ExcelUpload($_FILES['upload']);
        //标题
        $field = $upload->getExcelLineData(1);
        //内容
        $upData = $upload->getExcelDataList($field, 2);
        
        //批量插入
        $result = $this->product->mutiReplace($upData);
        print_r($result);
    }

    function index() {
        $products = $this->product->selectProductList();
        $this->assign['products'] = $products;
        $this->assign['include_page'] = __TPL_PATH__ . 'Product/index.html';
        $template_path = __TPL_PATH__ . 'index.html';
        $this->_view->display($template_path, $this->assign);
    }

    function add() {
        $this->assign['include_page'] = __TPL_PATH__ . 'Product/add.html';
        $template_path = __TPL_PATH__ . 'index.html';
        $this->_view->display($template_path, $this->assign);
    }
    
    function updatePros(){
        $data = Input::post('data');
        $data = json_decode($data, TRUE);
        /*$data = array(
            array('id' => '1', 'product_name' => '产品1', 'price' => '181', 'count' => '50'),
            array('id' => '2', 'product_name' => '产品2', 'price' => '182', 'count' => '52'),
            array('id' => '3', 'product_name' => '产品3', 'price' => '183', 'count' => '53'),
        );*/
        if (!empty($data)) {
            $ids = array();
            foreach ($data as $key => $value) {
                $ids[$key] = $value['id'];
            }
            $condition = " id in (" . implode(',', $ids) . ")";
            $result = $this->product->updateProducts($data, 'id', $condition);
            if ($result) {
                $this->jsonMsg(200, "修改产品成功", $result);
            } else {
                $this->jsonMsg(100, "修改产品失败");
            }
        }  else {
            $this->jsonMsg(100, "没有产品更改");
        }
    }

}
